﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using prjBULHRM;
namespace prjQuanLyNhanSu
{
    public partial class frmQT_LichLV : Form
    {
        public frmQT_LichLV()
        {
            InitializeComponent();
        }
        clsBULHT_Ca clsCa=new clsBULHT_Ca ();
        clsBULTT_PhongBan clsPhongBan = new clsBULTT_PhongBan();
        clsBULQT_LichLV clsQT_LichLV = new clsBULQT_LichLV();
        private void LoadDuLieu()
        {
            clsQT_LichLV = new clsBULQT_LichLV();
            dgrQT_LichLamViec.DataSource = clsQT_LichLV.SelectAll();

            cboCa.DataSource = clsCa.SelectAll();
            cboCa.DisplayMember = "TenCa";
            cboCa.ValueMember = "CaID";

            cboPhongBan.DataSource = clsPhongBan.SelectAll();
            cboPhongBan.DisplayMember = "Name";
            cboPhongBan.ValueMember = "PhongBan_ID";

            cboCa.Enabled = false;
            cboPhongBan.Enabled = false;
            cboCa.SelectedIndex = -1;
            cboPhongBan.SelectedIndex = -1;

        }
        private void frmQT_LichLV_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "&Thêm")
            {
                btnThem.Text = "&Lưu Thêm";
                cboCa.Enabled = true;
                cboCa.SelectedIndex = -1;
                cboPhongBan.Enabled = true;
                cboPhongBan.SelectedIndex = -1;
                btnXoa.Enabled = false;
                btnSua.Enabled = false;
            }
            else
            {
                if (cboCa.SelectedIndex == -1 || cboPhongBan.SelectedIndex == -1)
                {
                    MessageBox.Show("Bạn chưa chọn dữ liệu đầy đủ !", "Lỗi Dữ Liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    try
                    {
                        //clsQT_LichLV = new clsBULQT_LichLV();
                        btnThem.Text = "&Thêm";
                        clsQT_LichLV.CaID = cboCa.SelectedValue.ToString();
                        clsQT_LichLV.PhongBan = cboPhongBan.SelectedValue.ToString();
                        if (clsQT_LichLV.TrungMa() == true)
                        {
                            MessageBox.Show("Trùng Phòng ban !", "Trùng Mã", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {
                            clsQT_LichLV.Insert();
                            LoadDuLieu();
                            MessageBox.Show("Thêm thành công !");
                            btnXoa.Enabled = true;
                            btnSua.Enabled = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Không thêm được, bạn xem lại dữ liệu !","Lỗi Dữ Liệu",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void dgrQT_LichLamViec_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            cboCa.Enabled = false;
            cboPhongBan.Enabled = false;
            btnThem.Enabled = true;
            btnThem.Text = "&Thêm";
            btnXoa.Enabled = true;
            btnSua.Text = "&Sửa";
        }

        private void dgrQT_LichLamViec_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            cboPhongBan.Text = dgrQT_LichLamViec.CurrentRow.Cells[0].Value.ToString();
            cboCa.Text = dgrQT_LichLamViec.CurrentRow.Cells[1].Value.ToString();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "&Sửa")
            {
                if (cboCa.Text == "")
                {
                    MessageBox.Show("Bạn chưa chọn dữ liệu để sửa !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    btnSua.Text = "&Lưu Sửa";
                    cboPhongBan.Enabled = false;
                    cboCa.Enabled = true;
                    btnThem.Enabled = false;
                    btnXoa.Enabled = false;
                    dgrQT_LichLamViec.Enabled = false;
                }
            }
            else
            {
                try
                {
                    btnSua.Text = "&Sửa";
                    clsQT_LichLV.PhongBan = cboPhongBan.Text;
                    clsQT_LichLV.CaID = cboCa.SelectedValue.ToString();
                    if (cboCa.SelectedValue.ToString() == "")
                    {
                        MessageBox.Show("Bạn chưa chọn !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        clsQT_LichLV.Update();
                        LoadDuLieu();
                        MessageBox.Show("Sửa thành công !");
                        btnThem.Enabled = true;
                        btnXoa.Enabled = true;
                        dgrQT_LichLamViec.Enabled = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Không cho sửa, hãy xem lại dữ liệu !","Lỗi Dữ Liệu",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    btnThem.Enabled = true;
                    btnXoa.Enabled = true;
                    dgrQT_LichLamViec.Enabled = true;
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (cboCa.Text=="")
            {
                MessageBox.Show("Bạn chưa chọn mẫu tin để xóa !", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                try
                {
                    DialogResult hoi = MessageBox.Show("Bạn có chắc muốn xóa dữ liệu này không ?","Xóa Dữ Liệu",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                    if (hoi == DialogResult.Yes)
                    {
                        clsQT_LichLV.PhongBan = cboPhongBan.Text;
                        clsQT_LichLV.CaID = cboCa.Text;
                        clsQT_LichLV.Delete();
                        LoadDuLieu();
                        MessageBox.Show("Xóa thành công !");
                        cboCa.SelectedIndex = -1;
                        cboPhongBan.SelectedIndex = -1;
                    }                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Dữ liệu này không thể xóa được !","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                }
            }
        }
    }
}
